<?php

namespace app\logic\job;

use app\api\model\merchant\MerchantModel;
use app\api\model\merchant\MerchantOrderBill;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Log;
use ulit\CryptAES;
use yzh52521\EasyHttp\Http;

class Notify
{


    /***
     * @param $params
     * @param $merchant_id
     * @param $data
     * @return true|void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function run($params,$merchant_id,$data){


        $info=MerchantModel::where(['id' => $merchant_id])->field(['key','appid','secret'])->find();
        $aec = new CryptAES();
        $status = match ($data['status']) {
            'SUCCESS' => 1,
            'REFUND' => 6,
            'DEALING' => 2,
            default => 4,
        };
        $arr=[
            'order_sn'=>$data['out_biz_no'],
            'order_id'=>$data['order_id'],
            'status'=>$status,
            'trans_amount'=>$data['trans_amount'],
            'pay_date'=>$data['pay_date']
        ];
        $pasta=$aec->aesEncode(json_encode($arr,JSON_UNESCAPED_UNICODE),$info['key']);
        $send['code']=200;
        $send['msg']='success';
        $send['data']=$pasta;

        $result=Http::asJson()->post($params['notify_url'],$send);
        Log::write('第三方回调结果'.json_encode($result->array()));

        Log::write('第三方回调数据'.json_encode(['params'=>$params,'data'=>$arr],JSON_UNESCAPED_UNICODE));
        if($result->array()=='success'){
            Log::write('回调成功');
            return true;
        }
    }


}